<?php

namespace app\web\controller;

use app\common\controller\Frontend;
use app\common\model\purchase\Goods;
use app\common\model\Storehouse;
use app\common\model\supplier\Supplier;
use think\Config;
use think\Db;
use think\Hook;
use think\Validate;

class Enter extends Frontend
{

    protected $noNeedLogin = 'login';
    protected $noNeedRight = '*';
    protected $layout = '';
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\purchase\Enter;
    }
    public function index(){
            $where = [];
            if(session('admin')['storehouse_id'] >0){
                $where['o.storehouse_id'] =  session('admin')['storehouse_id'];
            }

            $rows = Db::name('purchase_enter')
                ->alias('o')
                ->field('o.id,o.createtime,o.number,o.status,s.nickname,o.operator,o.company,explain,abstract,actual_cost,h.name')
//                ->join('admin a','o.admin_id = a.id')
                ->join('supplier s','o.supplier_id = s.id')
                ->join('storehouse h','o.storehouse_id = h.id')
                ->where($where)
                ->order('o.id', 'DESC')
                ->limit(0, 10)
                ->select();

            $status = $this->model->enter_status();
            foreach ($rows as $k=> $v){
                $rows[$k]['ystatus'] = $rows[$k]['status'];
                $rows[$k]['status_txt'] = $status[$rows[$k]['status']];
//                $rows[$k]['arrival_total']=
            }

            $total = Db::name('purchase_order')->alias('o')
                ->join('supplier s','o.supplier_id = s.id')
                ->where($where)
                ->count();
            $this->assign('list',$rows);
            $this->assign('total',$total);
        return $this->view->fetch();
    }
    public function getorder(){
        $page = input('page/d',1);
        $status = input('status/d',0);
        $pagesize = input('pagesize/d',10);
        $where = [];
        if(session('admin')['storehouse_id'] >0){
            $where['o.storehouse_id'] =  session('admin')['storehouse_id'];
        }
        if(!empty($status)){
            $where['o.status'] = $status;
        }

        $rows = Db::name('purchase_enter')
            ->alias('o')
            ->field('o.id,o.createtime,o.number,o.status,s.nickname,o.operator,o.company,explain,abstract,actual_cost,h.name')
//                ->join('admin a','o.admin_id = a.id')
            ->join('supplier s','o.supplier_id = s.id')
            ->join('storehouse h','o.storehouse_id = h.id')
            ->where($where)
            ->order('o.id', 'DESC')
            ->limit($page*$pagesize, $pagesize)
            ->select();
        $status = $this->model->enter_status();
        foreach ($rows as $k=> $v){
            $rows[$k]['ystatus'] = $rows[$k]['status'];
            $rows[$k]['status_txt'] = $status[$rows[$k]['status']];
//                $rows[$k]['arrival_total']=
        }
        $this->success('','',$rows);
    }
    public function detail(){
        $ids = input('ids/d',0);
        $row = $this->model->find($ids);
        if(empty($row)){
            $this->error('数据不存在');
        }
        if($this->request->isAjax()){
            if($row['status'] !=1){
                $this->error('状态异常');
            }
            $status = input('status/d',0);
            Db::startTrans();
            try {
                $odata['status'] = $status;
                if ($status == 2) {
                    $type = 1;
                }
                if ($status == 5) {
                    $Goods = new Goods;
                    $Goods->save(array('status' => 5), array('purchase_order_id' => $ids));
                    $type = 2;
                    $list = Db::name('purchase_goods')->where(array('purchase_order_id'=>$ids))->select();
                    foreach ($list as $v){
                        //重置请购状态数据
                        Db::name('purchase_goods')->where(array('id'=>$v['id']))->update(array('status'=>1,'purchase_order_id'=>0,'unit_price'=>0,'ht_price'=>0));
                    }
                }
                Db::name('examine_log')->insert(array(
                    'admin_id'=>session('admin')['id'],
                    'type'=>$type,
                    'createtime'=>time(),
                    'controller'=>'purchase/order',
                    'source_id'=>$ids,
                    'source'=>1,
                    'remark'=>'采购单',
                    'number'=>$row['number'],
                ));
                $res = $this->model->save($odata, array('id' => $ids));
                if($res){
                    Db::commit();
                    return  array(
                        'code'=>1,
                        'msg'=>'审核成功',
                    );
                }
            }catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $this->error($e->getMessage());
            }
        }


        $Supplier = new Supplier;
        $srow = $Supplier->find($row['supplier_id']);
        $list = Db::name('purchase_enter_goods')
            ->where(array('purchase_enter_id'=>$ids))
            ->alias('p')
            ->join('goods g','p.goods_id=g.id')
            ->select();

        $this->assign('ids',$ids);
        $this->assign('list',$list);
        $this->assign('srow',$srow);
        $this->assign('row',$row);
        return $this->view->fetch();
    }
    public function enter(){
        $ids = input('ids/d',0);
        $row = $this->model->find($ids);
        if(empty($row)){
            $this->error('数据不存在');
        }
        $Supplier = new Supplier;
        $srow = $Supplier->find($row['supplier_id']);
        $list = Db::name('purchase_goods')
            ->where(array('purchase_order_id'=>$ids))
            ->alias('p')
            ->join('goods g','p.goods_id=g.id')
            ->select();
        foreach ($list as &$v){
            $cnum = Db::name('purchase_enter_goods')->where(array('goods_id'=>$v['goods_id'],'purchase_order_id'=>$ids))->sum('goods_num');
            $v['enter_num'] = $cnum;
        }
        $this->assign('ids',$ids);
        $this->assign('list',$list);
        $this->assign('srow',$srow);
        $this->assign('row',$row);
        return $this->view->fetch();
    }
}
